package com.xian.ucenter.controller.front;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xian.ucenter.commons.Result;
import com.xian.ucenter.entities.Post;
import com.xian.ucenter.entities.PostTag;
import com.xian.ucenter.entities.Tag;
import com.xian.ucenter.entities.vo.TagPostCount;
import com.xian.ucenter.entities.vo.TagPostVo;
import com.xian.ucenter.service.PostService;
import com.xian.ucenter.service.PostTagService;
import com.xian.ucenter.service.impl.TagServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/ucenter/front/tag")
public class TagFrontController {
    @Autowired
    private TagServiceImpl tagService;
    @Autowired
    private PostService postService;
    @Autowired
    private PostTagService postTagService;

    @RequestMapping("/{current}")
    public Result getTags(@PathVariable("current") Integer current) {
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        Page<Tag> page = pageList(current, 6, tagQueryWrapper);
        List<Tag> records = page.getRecords();
        List<TagPostCount> list = BeanUtil.copyToList(records, TagPostCount.class);
        list.forEach(tagPostCount -> tagPostCount.setCount(postTagService.count(new QueryWrapper<PostTag>().eq("tag_id",tagPostCount.getId()))));
        return Result.ok("list",list).data("total",page.getTotal()).data("current",page.getCurrent());
    }

    @RequestMapping("/{tagId}/{current}")
    public Result getTags(@PathVariable("tagId") String tagId, @PathVariable("current") Integer current) {
        Tag tag = tagService.getOne(new QueryWrapper<Tag>().eq("id", tagId));
        TagPostVo tagPostVo = BeanUtil.copyProperties(tag, TagPostVo.class);
        QueryWrapper<PostTag> postTagQueryWrapper = new QueryWrapper<>();
        postTagQueryWrapper.eq("tag_id",tagId);
        Page<PostTag> page = pagePostTagList(current, 6, postTagQueryWrapper);
        List<PostTag> list = page.getRecords();
        if (ObjectUtil.isNotNull(list) && list.size() > 0) {
            List<String> collect = list.stream().map(PostTag::getPostId).collect(Collectors.toList());
            List<Post> postList = postService.listByIds(collect).stream().filter(post -> post.getStatus()==0).collect(Collectors.toList());
            tagPostVo.setPostList(postList);
        }
        return Result.ok("item",tagPostVo).data("total",page.getTotal()).data("current",page.getCurrent());
    }

    public Page<Tag> pageList(Integer current, Integer count, Wrapper<Tag> wrapper) {
        Page<Tag> page = new Page<>(current,count);
        tagService.page(page,wrapper);
        return page;
    }

    public Page<PostTag> pagePostTagList(Integer current, Integer count, Wrapper<PostTag> wrapper) {
        Page<PostTag> page = new Page<>(current,count);
        postTagService.page(page,wrapper);
        return page;
    }
}
